package cn.zyb.basic.util;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

/**
 * @author zyb
 * @version 1.0.0
 * @date 2022年07月15日 10:49:20
 * @packageName cn.zyb.basic.util
 * @className SmsUtils
 * @describe TODO
 */
public class SmsUtils {
  //用户名
  public static final String UID = "清冽彼下泉";
  //秘钥
  public static final String KEY = "2D4B172500A670F6A01BD68D33D7C7E4";

  /**
   * 发送短信
   * @param phone 手机
   * @param content 发送内容
   * @return 返回值
   */
  public static String  sendSms(String phone,String content){
    PostMethod post = null;
    try {
      HttpClient client = new HttpClient();
      post = new PostMethod("http://utf8.api.smschinese.cn");
      post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf8");//在头文件中设置转码
      NameValuePair[] data ={ new NameValuePair("Uid", SmsUtils.UID),
        new NameValuePair("Key", SmsUtils.KEY),
        new NameValuePair("smsMob",phone),
        new NameValuePair("smsText",content)};
      post.setRequestBody(data);

      client.executeMethod(post);
      int statusCode = post.getStatusCode();
      System.out.println("statusCode:"+statusCode); //200 404 400
      String result = new String(post.getResponseBodyAsString().getBytes("utf8"));
      return result;

    } catch (Exception e) {
      e.printStackTrace();
    }
    finally {
      if (post != null) {
        post.releaseConnection();
      }
    }
    return null;
  }

  public static void main(String[] args) {
    System.out.println(SmsUtils.sendSms("19534950819", "您的验证码为：1111"));
  }
}
